import * as server from "@minecraft/server";
import * as data_item from "../data/item";
import * as derived_tools from "./derived_tools";
import * as native_tools from "./native_tools";
import * as math_tools from "./math_tools";
export { RealmPropertyName, CreateMineralVein, ExpendMineralVein, QueryMineralVein, ExpendEnergy, AlterEnergy, };
function RealmPropertyName(object, type, range) {
    const nodeQueue = [];
    server.world.getDynamicPropertyIds()
        .filter(node => node.startsWith(`${type}•`))
        .forEach(node => node.split(/•/)[1] == object.dimension.id
        ? nodeQueue.push({ x: JSON.parse(node.split(/•/)[2]), y: 0, z: JSON.parse(node.split(/•/)[4]) })
        : void 0);
    if (nodeQueue.length == 0)
        return;
    const distance = nodeQueue.map(node => math_tools.Vector.distance(node, { x: Math.floor(object.location.x / 16), y: 0, z: Math.floor(object.location.z / 16) }));
    const minDistance = Math.min(...distance);
    if (minDistance <= range) {
        const index = distance.indexOf(minDistance);
        return `${type}•${object.dimension.id}•${nodeQueue[index].x}•0•${nodeQueue[index].z}`;
    }
    else
        return;
}
;
function AlterEnergy(object, offset, create) {
    const current = `stardust_energy•${object.dimension.id}•${Math.floor(object.location.x / 16)}•0•${Math.floor(object.location.z / 16)}`;
    const realmName = RealmPropertyName(object, current.split(/•/)[0], 256);
    if (!realmName && create)
        server.world.setDynamicProperty(current, offset);
    if (!realmName)
        return [false, offset];
    const price = server.world.getDynamicProperty(realmName);
    if (price + offset <= 0) {
        server.world.setDynamicProperty(realmName, undefined);
        return [false, 0];
    }
    else if (price + offset >= 10000000) {
        return [true, price];
    }
    else {
        server.world.setDynamicProperty(realmName, price + offset);
        return [true, price + offset];
    }
}
;
function ExpendEnergy(block, modify, noShow) {
    const getEnergy = AlterEnergy(block, modify, false);
    if (getEnergy[0]) {
        if (!noShow)
            derived_tools.FloatingTextDisplay(block, '<§l§d 星尘力 §r> : §l§u' + getEnergy[1]);
        return true;
    }
    else {
        derived_tools.ErrorMessage('<§l§d 星尘力 §r>§4 发生错误§r', block, { text: '无法获取到足够的<§l§d 星尘力 §r>, 请使用<§l§u 能源节点 §r>进行补充或创建' });
        return false;
    }
}
;
function CreateMineralVein(realmName) {
    const output = { type: [], amount: [] };
    const value = Array.from(data_item.mineral_state.values()).map(value => math_tools.Random(value, true));
    const entry = Array.from(data_item.mineral_state.keys());
    for (let index = 0; index < math_tools.Random({ max: 9, min: 3 }, true); index++) {
        const randomIndex = math_tools.Random({ max: entry.length - 1, min: 0 }, true);
        output.amount.push(value[randomIndex]);
        output.type.push(entry[randomIndex]);
    }
    ;
    server.world.setDynamicProperty(realmName, JSON.stringify(output));
}
;
function QueryMineralVein(realmName) {
    const info = JSON.parse(server.world.getDynamicProperty(realmName));
    const output = [];
    for (let index = 0; index < info.type.length; index++) {
        output.push({ text: '§5§l' }, native_tools.BlockTranslate(info.type[index]), { text: `§r: §2${info.amount[index]}§r\n` });
    }
    ;
    return { rawtext: output };
}
;
function ExpendMineralVein(realmName) {
    const info = JSON.parse(server.world.getDynamicProperty(realmName));
    const randomIndex = math_tools.Random({ max: info.type.length - 1, min: 0 }, true);
    if (info.amount[randomIndex] <= 0)
        return 'minecraft:stone';
    info.amount[randomIndex] -= 1;
    server.world.setDynamicProperty(realmName, JSON.stringify(info));
    return info.type[randomIndex];
}
;
